package com.jy.wms.common.base;

import com.jy.wms.common.PropertyUtil;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by yjs on 2018/5/29.
 */
public class InterfaceCall {
    /**
     * 调用
     * @param json 参数
     * @param posturl 路径
     * @return
     * @throws Exception
     */
    public static JSONObject allocation(JSONObject json, String posturl) throws Exception {
        InputStream inss =InterfaceCall.class.getResourceAsStream("/createBean.properties");
        String url_file= PropertyUtil.getProperty("ssIp");
        URL url = new URL(url_file+posturl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setUseCaches(false);
        conn.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
        out.write(json.toString());
        out.flush();
        out.close();
        //获取响应内容体
        String line = "";
        String result = "";
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
        while ((line = in.readLine()) != null) {
            result += line + "\n";
        }
        in.close();
        JSONObject jsonObject = JSONObject.fromObject(result);
        System.out.println(jsonObject);
        return jsonObject;
    }

}
